package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.ButtonConfigDao;
import com.gccloud.design.entity.OptButtonBranchRelEntity;
import com.gccloud.design.entity.OptButtonEntity;
import com.gccloud.design.service.ButtonConfigService;
import com.gccloud.design.vo.OptButtonBranchVo;
import com.gccloud.design.vo.OptButtonVo;
import com.gccloud.starter.core.vo.PageVO;
import com.gccloud.starter.feign.api.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现功能【按钮配置业务实现层】
 *
 * @author hu.hongdou
 * @date 2022/2/21
 */
@Service
public class ButtonConfigServiceImpl extends ServiceImpl<ButtonConfigDao, OptButtonEntity> implements ButtonConfigService {

    @Resource
    private ButtonConfigDao buttonConfigDao;

    @Resource
    private ISysUserService iSysUserService;

    @Override
    public Map<String, Object> newQueryButton(String buttonName, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(3);
        Page<OptButtonVo> page = new Page<>();
        page.setCurrent(pageObj.getCurrent()).setSize(pageObj.getSize());
        map.put("list", buttonConfigDao.newQueryButton(buttonName, page));
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> newQueryButtonBranch(Integer buttonId, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(3);
        Page<OptButtonBranchVo> page = new Page<>();
        page.setSize(pageObj.getSize()).setCurrent(pageObj.getCurrent());
        map.put("list", buttonConfigDao.newQueryButtonBranch(buttonId, page));
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public boolean newAddButton(OptButtonEntity entity) {
        try {
            int result = buttonConfigDao.newAddButton(entity);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------新增按钮失败，" + e.getMessage());
            return false;
        }

    }

    @Override
    public boolean newModifyButton(OptButtonEntity entity) {
        try {
            int result = buttonConfigDao.newModifyButton(entity);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------更新按钮失败，" + e.getMessage());
            return false;
        }
    }

    @Override
    public boolean newDeleteButton(Long buttonId) {
        boolean result = true;
        try {
            //删除opt_button_branch_rel表
            buttonConfigDao.newDeleteButtonBranchRelByBtnId(buttonId);
            //删除OPT_BUTTON表
            buttonConfigDao.newDeleteButtonByBtnId(buttonId);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
            log.error("---------------------------删除按钮失败，" + e.getMessage());
        }
        iSysUserService.getCurrentUser();
        return result;
    }

    @Override
    public boolean newAddButtonBranch(OptButtonBranchRelEntity entity) {
        int result = 0;
        try {
            result = buttonConfigDao.newAddButtonBranchRel(entity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------新增按钮分支关系失败，" + e.getMessage());
        }
        return result > 0;
    }

    @Override
    public boolean newDeleteButtonBranchRel(int buttonId, int optBranchId) {
        int result = 0;
        try {
            result = buttonConfigDao.newDeleteButtonBranchRelByBtnIdAndBranchId(buttonId, optBranchId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------删除按钮分支关系失败，" + e.getMessage());
        }
        return result > 0;
    }

    @Override
    public Map<String, Object> queryBusinessType() {
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", buttonConfigDao.queryBusinessType());
        return map;
    }

    @Override
    public Map<String, Object> queryOptButtonBranch(Integer buttonId, String businessTypeId) {
        Map<String, Object> map = new HashMap<>(8);
        List<OptButtonBranchVo> dataList = buttonConfigDao.queryOptButtonBranch(buttonId, businessTypeId);
        map.put("list", dataList);
        return map;
    }

    @Override
    public Map<String, Object> getLinkButton(String tacheId, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(8);
        Page<OptButtonVo> page = new Page<>();
        page.setCurrent(pageObj.getCurrent()).setSize(pageObj.getSize());
        map.put("list", buttonConfigDao.getLinkButton(tacheId, page));
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> queryButtonByTacheId(String tacheId, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(4);
        Page<OptButtonVo> page = new Page<>();
        page.setCurrent(pageObj.getCurrent()).setSize(pageObj.getSize());
        map.put("list", buttonConfigDao.queryButtonByTacheId(tacheId, page));
        map.put("total", page.getTotal());
        return map;
    }
}
